package Prova::Controller::File;

use strict;
use warnings;
use base 'Catalyst::Controller';
use Data::Dumper;

=head1 NAME

Prova::Controller::File - Catalyst Controller

=head1 SYNOPSIS

See L<Prova>

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut

=head2 create

create action

=cut

sub create : Private {
    my ( $self, $c ) = @_;

    my $datablock = $c->stash->{datablock};

    if (defined $datablock) {
        my $file;
        $file->{name} = $c->stash->{file}->{name};
        $file->{extention} = $c->stash->{file}->{extention};
        $file->{size} = $c->stash->{file}->{size};
        $file->{body} = $c->stash->{file}->{body};
        $file->{data_block_id} = $datablock->id;
        my $model = $c->model('DBIC::File')->create($file);
        $model->path(sprintf(
            "--- t/testfiles/%d_%d_%s.%s\n",
            $datablock->id,
            $model->id,
            $model->name,
            $model->extention
        ));
        $model->update();
        undef $c->stash->{file};
        $c->stash->{file} = $model;
    }
}

=head2 find_by_id

get accessor models having datablock_id

=cut

sub find_by_id : Private {
    my ( $self, $c, $datablock_id ) = @_;

    my $datasection = [ $c->model( 'DBIC::File' )->search(
        { data_block_id => $datablock_id },
        { order_by => 'id' }
    ) ];

    return $datasection;
}

=head1 AUTHOR

Shanon Inc, E<lt>http://www.shanon.co.jp/E<gt>

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

1;
